﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Watson.Core.Tools;
using ITOMMS.Entity;
using ITOMMS.Business;

namespace ITOMMS.Web.InsideManage
{
    public partial class WorkLogAdd : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("RoleGroup!=" + PubEnums.RoleGroup.客户.ToString("D")), ddl姓名, "ID", "RealName");//内部员工的姓名
                DropDownListHelper.InitDropList<tbDepartmentEntity>(new tbDepartmentEntity().GetAll(), ddl部门, "ID", "DepartmentName");
                
                if (Request.Params["id"] != null)
                {
                    string sid = Request.Params["id"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        DataShow(pid);
                    }
                }

                if (Request.Params["vid"] != null)
                {
                    string sid = Request.Params["vid"].ToString();
                    btnSave.Visible = false;
                    btnBack.Visible = true;
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        DataShow(pid);
                    }
                }
            }
        }


        private void DataShow(int troubleId)
        {
            tb工作日志表Entity entity = new tb工作日志表Entity();
            entity = entity.GetById(troubleId);

            if (entity != null)
            {
                ViewState["Entity"] = entity;


                //ddl姓名.SelectedValue = entity.姓名.ToString();
                //ddl部门.SelectedValue = entity.部门.ToString();
                txt职位.Text = entity.职位;
                if (entity.日期.HasValue)
                {
                    txt日期.Text = entity.日期.Value.ToShortDateString();
                }
                txt客户名称.Text = entity.客户名称;
                ddl重要级别.SelectedValue = entity.重要级别;
                this.ddl工作性质.Items.FindByValue(entity.工作性质).Selected = true;
                if (entity.接报时间.HasValue)
                {
                    txt接报时间.Text = entity.接报时间.Value.ToString("yyyy-MM-dd HH:mm:ss");
                }

                if (entity.到场时间.HasValue)
                {
                    txt到场时间.Text = entity.到场时间.Value.ToString("yyyy-MM-dd HH:mm:ss");
                }



                txt消耗时间.Text = entity.消耗时间;

                txt工作内容.Text = entity.工作内容;

            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                Entity.tb工作日志表Entity entity = new ITOMMS.Entity.tb工作日志表Entity();
                if (ViewState["Entity"] != null)
                {
                    entity = ViewState["Entity"] as tb工作日志表Entity;
                }
                else
                {
                    entity.CreateBy = SessionHelper.UserID;
                    entity.CreateDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }

                entity.姓名 = int.Parse(ddl姓名.SelectedValue);
                entity.部门 = int.Parse(ddl部门.SelectedValue);
                entity.职位 = txt职位.Text;
                DateTime rs = new DateTime();
                if (DateTime.TryParse(txt日期.Text.Trim(), out  rs))
                {
                    entity.日期 = rs;
                }


                entity.客户名称 = txt客户名称.Text;
                entity.重要级别 = ddl重要级别.SelectedValue;
                entity.工作性质 = this.ddl工作性质.SelectedValue;

                DateTime jbtime = new DateTime();
                if (DateTime.TryParse(txt接报时间.Text.Trim(), out  jbtime))
                {
                    entity.接报时间 = jbtime;
                }
                DateTime dctime = new DateTime();
                if (DateTime.TryParse(txt到场时间.Text.Trim(), out  dctime))
                {
                    entity.到场时间 = dctime;
                }


               entity.消耗时间 = txt消耗时间.Text.Trim();
               
                entity.工作内容 = txt工作内容.Text;
                entity.ModifyBy = SessionHelper.UserID;
                entity.ModifyDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                entity.SaveOrUpdate(entity);
                ViewState["Entity"] = entity;
                MessageboxInfo.MessageBox(this, MessageType.SUCCEEDED, "保存成功");
            }
            catch (Exception ex)
            {
                log4netHelper.error(ex);
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "保存失败");
            }

        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("WorkLogList.aspx", true);
        }
    }
}
